package com.cutie.user.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cutie.core.util.ServletUtils;
import com.cutie.user.model.User;
import com.cutie.user.service.UserService;
import com.mysql.jdbc.StringUtils;

public class LoginServlet extends HttpServlet {

	UserService userService;
	
	
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		userService = new UserService();
	}
	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletUtils.setEncoding(request, response);
		PrintWriter out = response.getWriter();
		
		
		try{
			String name = (String)request.getParameter("name");
			String password = (String)request.getParameter("password");
			
			if(StringUtils.isNullOrEmpty(name) ||
			   StringUtils.isNullOrEmpty(password) ){
				out.println("参数不合法");
				out.flush();
				out.close();
				return;
			}
			
			User user = new User();
			user.setName(name);
			user.setPassword(password);
			
			String status = userService.login(user);
			out.println(status);
			
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
